#include "userinfowidget.h"
#include "mainwidget.h"
#include "model/datacenter.h"
#include <QGridLayout>
#include <QMessageBox>

using namespace model;

UserInfoWidget::UserInfoWidget(const UserInfo &userInfo, QWidget *parent)
    : QDialog(parent)
    , userInfo(userInfo)
{
    // 1. 设置基本属性
    this->setFixedSize(400, 200);
    this->setWindowTitle("用户详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->move(QCursor::pos());

    // 2. 创建布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setVerticalSpacing(10);
    layout->setHorizontalSpacing(20);
    layout->setContentsMargins(40, 20, 0, 0);
    layout->setAlignment(Qt::AlignTop);
    this->setLayout(layout);

    // 3. 添加头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(75, 75);
    avatarBtn->setIconSize(QSize(75, 75));
    avatarBtn->setIcon(userInfo.avatar);

    QString labelStyle = "QLabel { font-weight: 800; padding-left: 20px;}";
    QString btnStyle = "QPushButton { border: 1px solid rgb(100, 100, 100); border-radius: 5px; background-color: rgb(240, 240, 240); }";
    btnStyle += "QPushButton:pressed { background-color: rgb(205, 205, 205); }";

    int width = 80;
    int height = 30;

    // 4. 添加用户序号
    idTag = new QLabel();
    idTag->setText("序号");
    idTag->setStyleSheet(labelStyle);
    idTag->setFixedSize(width, height);
    idTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    idLabel = new QLabel();
    idLabel->setText(userInfo.userID);
    idLabel->setFixedSize(width, height);
    idLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 5. 添加用户昵称
    nameTag = new QLabel();
    nameTag->setText("昵称");
    nameTag->setStyleSheet(labelStyle);
    nameTag->setFixedSize(width, height);
    nameTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    nameLabel = new QLabel();
    nameLabel->setText(userInfo.nickname);
    nameLabel->setFixedSize(width, height);
    nameLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 6. 设置电话
    phoneTag = new QLabel();
    phoneTag->setText("电话");
    phoneTag->setStyleSheet(labelStyle);
    phoneTag->setFixedSize(width, height);
    phoneTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    phoneLabel = new QLabel();
    phoneLabel->setText(userInfo.phone);
    phoneLabel->setFixedSize(width, height);
    phoneLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 7. 添加功能按钮
    applyBtn = new QPushButton();
    applyBtn->setText("申请好友");
    applyBtn->setFixedSize(80, 30);
    applyBtn->setStyleSheet(btnStyle);
    applyBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    sendMessageBtn = new QPushButton();
    sendMessageBtn->setText("发送消息");
    sendMessageBtn->setFixedSize(80, 30);
    sendMessageBtn->setStyleSheet(btnStyle);
    sendMessageBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    deleteFriendBtn = new QPushButton();
    deleteFriendBtn->setText("删除好友");
    deleteFriendBtn->setFixedSize(80, 30);
    deleteFriendBtn->setStyleSheet(btnStyle);
    deleteFriendBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 8. 添加上述内容到布局管理器中
    layout->addWidget(avatarBtn, 0, 0, 3, 1);

    layout->addWidget(idTag, 0, 1);
    layout->addWidget(idLabel, 0, 2);

    layout->addWidget(nameTag, 1, 1);
    layout->addWidget(nameLabel, 1, 2);

    layout->addWidget(phoneTag, 2, 1);
    layout->addWidget(phoneLabel, 2, 2);

    layout->addWidget(applyBtn, 3, 0);
    layout->addWidget(sendMessageBtn, 3, 1);
    layout->addWidget(deleteFriendBtn, 3, 2);

    //9. 设置按钮的可见性 初始化按钮禁用关系
    //如果是好友 就将申请好友按钮禁用
    DataCenter* dataCenter = DataCenter::getInstance();
    UserInfo* myFriend = dataCenter->findFriendById(userInfo.userID);
    if(myFriend == nullptr)  //不是好友
    {
        applyBtn->setEnabled(true);
        sendMessageBtn->setEnabled(false);
        deleteFriendBtn->setEnabled(false);
    }
    else    //是好友
    {
        applyBtn->setEnabled(false);
        sendMessageBtn->setEnabled(true);
        deleteFriendBtn->setEnabled(true);
    }

    //10. 初始化信号槽
    this->initSignalSlots();

}

void UserInfoWidget::initSignalSlots()
{
    //点击<发送消息>按钮时 自动跳转到对应会话
    connect(sendMessageBtn, &QPushButton::clicked, [this]()
    {
        //拿到主窗口中 实现的切换到会话
        MainWidget* mainWidget = MainWidget::getInstance();
        mainWidget->switchChatSession(this->userInfo.userID);

        //关闭当前窗口
        this->close();
    });
    //点击<删除好友>按钮时
    connect(deleteFriendBtn, &QPushButton::clicked, this, &UserInfoWidget::clickDeleteFriendBtn);
    //点击<申请好友>按钮时
    connect(applyBtn, &QPushButton::clicked, this, &UserInfoWidget::clickApplyFriendBtn);
}

void UserInfoWidget::clickDeleteFriendBtn()
{
    //1.弹出对话框 再让用户确认一遍是否要删除
    auto result = QMessageBox::warning(this, "确认删除", "确认删除当前好友？", QMessageBox::Yes | QMessageBox::Cancel);
    if(result != QMessageBox::Yes)
    {
        //用户取消删除
        return;
    }
    //2.调用dataCenter删除好友接口
    DataCenter* dataCenter = DataCenter::getInstance();
    //由于此处信号的处理 都是由主窗口这里进行的 所以这里只需要调用删除好友的异步接口即可
    dataCenter->deleteFriendAsync(this->userInfo.userID);

    //3.关闭当前窗口
    this->close();
}

void UserInfoWidget::clickApplyFriendBtn()
{
    //1. 发送好友申请
    LOG() << "发送好友申请 对方userID=" << userInfo.userID;
    DataCenter* dataCenter = DataCenter::getInstance();
    //不能加自己好友
    if(userInfo.userID == dataCenter->getMyself()->userID)
    {
        QMessageBox::information(this, "提示", "不能添加自己为好友!");
        return;
    }
    dataCenter->addFriendApplyAsync(userInfo.userID);
    //3. 关闭当前窗口
    this->close();
}













